/*
 * SearchModel.java
 * business
 *
 * Created by ChenTao on 2016/9/9.
 *
 * Copyright (c) 2016年 yidiandao. All rights reserved.
 */

package com.yidiandao.business.data.model;

import com.yidiandao.business.constant.Common;
import com.yidiandao.business.constant.Urls;
import com.yidiandao.business.data.bean.Goods;
import com.yidiandao.business.data.bean.Shop;
import com.yidiandao.business.data.bean.Tag;
import com.yidiandao.business.data.net.NetBiz;
import com.yidiandao.business.data.net.NetListener;
import com.yidiandao.business.data.net.exception.BizErrorInfo;
import com.yidiandao.business.data.net.exception.NetErrorInfo;
import com.yidiandao.business.data.recyclertype.DoubleGoods;
import com.yidiandao.business.data.request.SearchPriceRequest;
import com.yidiandao.business.data.request.SearchRequest;
import com.yidiandao.business.data.request.SearchSalesRequest;
import com.yidiandao.business.data.response.SearchGoodsHotResponse;
import com.yidiandao.business.data.response.SearchGoodsInfoResponse;
import com.yidiandao.business.data.response.SearchShopInfoResponse;
import com.yidiandao.business.ui.widget.listview.DisplayListItem;
import com.yidiandao.business.ui.widget.listview.Functional;
import com.yidiandao.business.ui.widget.listview.displaylistitem.ShopListItem;
import com.yidiandao.business.utils.PreferenceUtils;
import com.yidiandao.business.utils.UIUtils;

import java.util.ArrayList;
import java.util.List;

public class SearchModel implements ISearchModel {

    private NetBiz mNetBiz = new NetBiz();
    private List<DoubleGoods> mDoubleGoodsList = new ArrayList<>();

    private int mGoodsPager;
    private List<Goods> mGoodsList = new ArrayList<>();

    @Override
    public void getGoodsHot(String pagerTag, int type, final onLoadNetListener listener) {
        SearchRequest request = new SearchRequest();
        request.setUrl(Urls.GET_HOT_SEARCH);
        request.setTag(pagerTag);
        request.setType(type);
        request.setToken(PreferenceUtils.getToken(UIUtils.getContext()));
        mNetBiz.sendGetRequest(
                request,
                SearchGoodsHotResponse.class,
                new NetListener<SearchGoodsHotResponse>() {
                    @Override
                    public void onSuccess(SearchGoodsHotResponse searchGoodsHotResponse) {
                        List<String> data = searchGoodsHotResponse.getData();
                        List<Tag> tags = new ArrayList<>();
                        for (int i = 0; i < data.size(); i++) {
                            Tag tag = new Tag(data.get(i));
                            tags.add(tag);
                        }
                        if (listener != null) {
                            listener.onSuccess(tags);
                        }
                    }

                    @Override
                    public boolean onError(NetErrorInfo netErrorInfo) {
                        if (listener != null) {
                            listener.onFailed();
                        }
                        return true;
                    }

                    @Override
                    public boolean onFailed(BizErrorInfo bizErrorInfo) {
                        if (listener != null) {
                            listener.onFailed();
                        }
                        return true;
                    }

                    @Override
                    public void onFinal() {
                        if (listener != null) {
                            listener.onFinal();
                        }
                    }
                });
    }

    @Override
    public void loadNewGoodsList(
            final boolean isRefreshing,
            String pagerTag,
            String keyword,
            int type,
            final OnLoadSearchGoodsListener listener) {
        // 如果下拉刷新，则 还有更多数据 page = 1
        if (isRefreshing) {
            mGoodsPager = 1;
        }
        SearchRequest request = new SearchRequest();
        request.setUrl(Urls.SEARCH);
        request.setTag(pagerTag);
        request.setKeyword(keyword);
        request.setType(type);
        request.setPageSize(Common.PAGER_SIZE);
        request.setPage(mGoodsPager);

        request.setToken(PreferenceUtils.getToken(UIUtils.getContext()));
        mNetBiz.sendGetRequest(
                request,
                SearchGoodsInfoResponse.class,
                new NetListener<SearchGoodsInfoResponse>() {
                    @Override
                    public void onSuccess(SearchGoodsInfoResponse response) {
                        // pager 增加
                        mGoodsPager++;
                        List<Goods> goodsLines = response.getData().getGoodsList();
                        if (isRefreshing) {
                            mDoubleGoodsList.clear();
                            mGoodsList.clear();
                        }
                        mGoodsList.addAll(goodsLines);

                        for (int i = 0; i < goodsLines.size(); i += 2) {
                            Goods goods1 = null;
                            Goods goods2 = null;
                            try {
                                goods1 = goodsLines.get(i);
                                goods2 = goodsLines.get(i + 1);
                            } catch (IndexOutOfBoundsException e) {
                            }
                            mDoubleGoodsList.add(new DoubleGoods(goods1, goods2));
                        }

                        if (listener != null) {
                            listener.onSuccess(
                                    mGoodsList,
                                    mDoubleGoodsList,
                                    goodsLines.size() == 0,
                                    response.hasMoreData());
                        }
                    }

                    @Override
                    public boolean onError(NetErrorInfo netErrorInfo) {
                        if (listener != null) {
                            listener.onFailed();
                        }
                        return true;
                    }

                    @Override
                    public boolean onFailed(BizErrorInfo bizErrorInfo) {
                        if (listener != null) {
                            listener.onFailed();
                        }
                        return true;
                    }

                    @Override
                    public void onFinal() {
                        if (listener != null) {
                            listener.onFinal();
                        }
                    }
                });
    }

    @Override
    public void loadPriceGoodsList(
            final boolean isRefreshing,
            String pagerTag,
            boolean isSortUp,
            String keyword,
            int type,
            final OnLoadSearchGoodsListener listener) {
        // 如果下拉刷新，则 还有更多数据 page = 1
        if (isRefreshing) {
            mGoodsPager = 1;
        }
        SearchPriceRequest request = new SearchPriceRequest();
        request.setUrl(Urls.SEARCH);
        request.setTag(pagerTag);
        request.setKeyword(keyword);
        request.setType(type);
        request.setPageSize(Common.PAGER_SIZE);
        request.setPage(mGoodsPager);
        request.setPrice(isSortUp ? SearchPriceRequest.SORT_UP : SearchPriceRequest.SORT_DOWN);
        request.setToken(PreferenceUtils.getToken(UIUtils.getContext()));
        mNetBiz.sendGetRequest(
                request,
                SearchGoodsInfoResponse.class,
                new NetListener<SearchGoodsInfoResponse>() {
                    @Override
                    public void onSuccess(SearchGoodsInfoResponse response) {
                        // pager 增加
                        mGoodsPager++;
                        List<Goods> goodsLines = response.getData().getGoodsList();
                        if (isRefreshing) {
                            mDoubleGoodsList.clear();
                            mGoodsList.clear();
                        }
                        mGoodsList.addAll(goodsLines);

                        for (int i = 0; i < goodsLines.size(); i += 2) {
                            Goods goods1 = null;
                            Goods goods2 = null;
                            try {
                                goods1 = goodsLines.get(i);
                                goods2 = goodsLines.get(i + 1);
                            } catch (IndexOutOfBoundsException e) {
                            }
                            mDoubleGoodsList.add(new DoubleGoods(goods1, goods2));
                        }

                        if (listener != null) {
                            listener.onSuccess(
                                    mGoodsList,
                                    mDoubleGoodsList,
                                    goodsLines.size() == 0,
                                    response.hasMoreData());
                        }
                    }

                    @Override
                    public boolean onError(NetErrorInfo netErrorInfo) {
                        if (listener != null) {
                            listener.onFailed();
                        }
                        return true;
                    }

                    @Override
                    public boolean onFailed(BizErrorInfo bizErrorInfo) {
                        if (listener != null) {
                            listener.onFailed();
                        }
                        return true;
                    }

                    @Override
                    public void onFinal() {
                        if (listener != null) {
                            listener.onFinal();
                        }
                    }
                });
    }

    @Override
    public void loadSalesGoodsList(
            final boolean isRefreshing,
            String pagerTag,
            String keyword,
            int type,
            final OnLoadSearchGoodsListener listener) {
        // 如果下拉刷新，则 还有更多数据 page = 1
        if (isRefreshing) {
            mGoodsPager = 1;
        }
        SearchSalesRequest request = new SearchSalesRequest();
        request.setUrl(Urls.SEARCH);
        request.setTag(pagerTag);
        request.setKeyword(keyword);
        request.setType(type);
        request.setPageSize(Common.PAGER_SIZE);
        request.setPage(mGoodsPager);
        request.setSalesNum(SearchPriceRequest.SORT_DOWN);
        request.setToken(PreferenceUtils.getToken(UIUtils.getContext()));
        mNetBiz.sendGetRequest(
                request,
                SearchGoodsInfoResponse.class,
                new NetListener<SearchGoodsInfoResponse>() {
                    @Override
                    public void onSuccess(SearchGoodsInfoResponse response) {
                        // pager 增加
                        mGoodsPager++;
                        List<Goods> goodsLines = response.getData().getGoodsList();
                        if (isRefreshing) {
                            mDoubleGoodsList.clear();
                            mGoodsList.clear();
                        }
                        mGoodsList.addAll(goodsLines);

                        for (int i = 0; i < goodsLines.size(); i += 2) {
                            Goods goods1 = null;
                            Goods goods2 = null;
                            try {
                                goods1 = goodsLines.get(i);
                                goods2 = goodsLines.get(i + 1);
                            } catch (IndexOutOfBoundsException e) {
                            }
                            mDoubleGoodsList.add(new DoubleGoods(goods1, goods2));
                        }

                        if (listener != null) {
                            listener.onSuccess(
                                    mGoodsList,
                                    mDoubleGoodsList,
                                    goodsLines.size() == 0,
                                    response.hasMoreData());
                        }
                    }

                    @Override
                    public boolean onError(NetErrorInfo netErrorInfo) {
                        if (listener != null) {
                            listener.onFailed();
                        }
                        return true;
                    }

                    @Override
                    public boolean onFailed(BizErrorInfo bizErrorInfo) {
                        if (listener != null) {
                            listener.onFailed();
                        }
                        return true;
                    }

                    @Override
                    public void onFinal() {
                        if (listener != null) {
                            listener.onFinal();
                        }
                    }
                });
    }

    @Override
    public void searchShop(
            String pagerTag,
            String keyword,
            int type,
            int page,
            final onSearchShopsListener listener) {
        SearchRequest request = new SearchRequest();
        request.setUrl(Urls.SEARCH);
        request.setTag(pagerTag);
        request.setKeyword(keyword);
        request.setType(type);
        request.setToken(PreferenceUtils.getToken(UIUtils.getContext()));

        mNetBiz.sendGetRequest(
                request,
                SearchShopInfoResponse.class,
                new NetListener<SearchShopInfoResponse>() {
                    @Override
                    public void onSuccess(SearchShopInfoResponse response) {
                        int totalPage = response.getPagination().getTotal();

                        SearchShopInfoResponse.DataBean data = response.getData();

                        List<Shop> shopList = data.getSupplierList();
                        List<DisplayListItem> displayListItems = new ArrayList<>();
                        if (shopList.size() != 0) {
                            List<DisplayListItem> listItems =
                                    Functional.serverToClient(
                                            shopList,
                                            new Functional.Processor<Shop, DisplayListItem>() {
                                                @Override
                                                public DisplayListItem processToClient(Shop shop) {
                                                    return new ShopListItem(shop);
                                                }
                                            });
                            displayListItems.addAll(listItems);
                        }

                        if (listener != null) {
                            listener.onSuccess(totalPage, displayListItems);
                        }
                    }

                    @Override
                    public boolean onError(NetErrorInfo netErrorInfo) {
                        if (listener != null) {
                            listener.onFailed();
                        }
                        return false;
                    }

                    @Override
                    public boolean onFailed(BizErrorInfo bizErrorInfo) {
                        if (listener != null) {
                            listener.onFailed();
                        }
                        return false;
                    }

                    @Override
                    public void onFinal() {
                        if (listener != null) {
                            listener.onFinal();
                        }
                    }
                });
    }
}
